<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>if测试</title>
    <style>
        div {
            width: 100%;
            height: 500px;
            /* background-color: black; */
        }

        #macd {
            width: 100%;
            height: 300px;
            background-color: rgb(19, 18, 18);
        }
    </style>
</head>

<body style="background-color: black;">

    <div id="box"> </div>
    <div id="macd"></div>


    <script src="./utils/echarts.min.js"></script>
    <script src="./utils/jquery-3.6.0.js"></script>
    <script>
        setInterval(function () {
            window.location.reload(true)
        }, 10000)//刷新页面 暂时用此方法需要改进
        var arrPrice = []
        $.ajax({
            url: './ipit.txt', /* url:请求地址 */
            type: 'get', /* type:请求类型：post/get */
            // data: 'a=1&b=2',
            dataType: 'text', /* dataType:服务器返回的数据类型 text/xml/script/html/json/jsonp */
            success: function (data) { /* 回调函数 */
                var arr = data.split('\n')//把字符串转化成数组  
                //先分离标题
                var arrNtitle = arr.splice(1)//只有时间和价格的数组
                var arrUsed = []
                for (let i = 0, length = arrNtitle.length; i < length; i++) {
                    arrUsed.push(arrNtitle[i].split(','))
                }
                // 把时间提取出来变成一个单独数组 并和价格数据一一对应
                var arrData = [] //纯时间坐标
                arrUsed.forEach(function (el) {//截取时间
                    arrData.push(el.shift())
                })
                arrUsed.forEach(function (el) {
                    let a = []
                    el.map(function (el) {
                        a.push(parseInt(el * 10) / 10) //转化成一位小数的小数
                    })
                    let [op, cp, hp, lp] = a  //解构赋值 只要有用的前四位元素
                    arrPrice.push([op, cp, hp, lp])
                })
                for (let i = 0; i < arrData.length; i++) {
                    arrPrice[i].unshift(arrData[i])//把data加入元素 一一对应
                }


                //这里如果用vue来处理数据可以把数据直接给vue实例的data 然后进行数据的绑定
                charts(arrPrice)
                //执行echarts画图函数
                // console.log(arrPrice);
                let lowerPrice=[]
                arrPrice.forEach(function(el,i){
                    lowerPrice.push(el[2])
                })
                    
               console.log(lowerPrice);

               for(let i=0,l=lowerPrice.length;i<l;i++){
                    max=max[i]<max[i+1] ? max[i+1]:max[i]
                    
               }
                
            }
        });
      
        //最低价过滤 3
        





        function charts(data) {//k线图代码  封装成一个函数 用data接收处理好的数据 在上面调用
            var chartDom = document.getElementById('box');//导入容器
            var myChart = echarts.init(chartDom);
            var option;
            var upColor = '#ec0000';
            var upBorderColor = '#8A0000';
            var downColor = '#00da3c';
            var downBorderColor = '#008F28';
            // 数据意义：开盘(open)，收盘(close)，最低(lowest)，最高(highest)
            var data0 = splitData(
                data//数据
            );
            function splitData(rawData) {
                var categoryData = [];
                var values = []
                for (var i = 0; i < rawData.length; i++) {
                    categoryData.push(rawData[i].splice(0, 1)[0]);
                    values.push(rawData[i])
                }
                return {
                    categoryData: categoryData,
                    values: values
                };
            }

            function calculateMA(dayCount) {
                var result = [];
                for (var i = 0, len = data0.values.length; i < len; i++) {
                    if (i < dayCount) {
                        result.push('-');
                        continue;
                    }
                    var sum = 0;
                    for (var j = 0; j < dayCount; j++) {
                        sum += data0.values[i - j][1];
                    }
                    result.push(sum / dayCount);
                }
                return result;
            }

            option = {
                title: {
                    text: 'IF分钟指数',
                    left: 0
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    }
                },
                legend: {
                    data: ['日K', 'MA5', 'MA10', 'MA20', 'MA30']
                },
                grid: {
                    left: '10%',
                    right: '10%',
                    bottom: '15%'
                },
                xAxis: {
                    type: 'category',
                    data: data0.categoryData,
                    scale: true,
                    boundaryGap: false,
                    axisLine: { onZero: false },
                    splitLine: { show: false },
                    splitNumber: 20,
                    min: 'dataMin',
                    max: 'dataMax'
                },
                yAxis: {
                    scale: true,
                    splitArea: {
                        show: true
                    }
                },
                dataZoom: [
                    {
                        type: 'inside',
                        start: 50,
                        end: 100
                    },
                    {
                        show: true,
                        type: 'slider',
                        top: '90%',
                        start: 50,
                        end: 100
                    }
                ],
                series: [
                    {
                        name: '日K',
                        type: 'candlestick',
                        data: data0.values,
                        itemStyle: {
                            color: upColor,
                            color0: downColor,
                            borderColor: upBorderColor,
                            borderColor0: downBorderColor
                        },
                        markPoint: {
                            label: {
                                normal: {
                                    formatter: function (param) {
                                        return param != null ? Math.round(param.value) : '';
                                    }
                                }
                            },
                            data: [
                                {
                                    name: 'XX标点',
                                    coord: ['2021/9/09', 3300],
                                    value: 3300,
                                    itemStyle: {
                                        color: 'rgb(41,60,85)'
                                    }
                                },
                                {
                                    name: 'highest value',
                                    type: 'max',
                                    valueDim: 'highest'
                                },
                                {
                                    name: 'lowest value',
                                    type: 'min',
                                    valueDim: 'lowest'
                                },
                                {
                                    name: 'average value on close',
                                    type: 'average',
                                    valueDim: 'close'
                                }
                            ],
                            tooltip: {
                                formatter: function (param) {
                                    return param.name + '<br>' + (param.data.coord || '');
                                }
                            }
                        },
                        markLine: {
                            symbol: ['none', 'none'],
                            data: [
                                [
                                    {
                                        name: 'from lowest to highest',
                                        type: 'min',
                                        valueDim: 'lowest',
                                        symbol: 'circle',
                                        symbolSize: 10,
                                        label: {
                                            show: false
                                        },
                                        emphasis: {
                                            label: {
                                                show: false
                                            }
                                        }
                                    },
                                    {
                                        type: 'max',
                                        valueDim: 'highest',
                                        symbol: 'circle',
                                        symbolSize: 10,
                                        label: {
                                            show: false
                                        },
                                        emphasis: {
                                            label: {
                                                show: false
                                            }
                                        }
                                    }
                                ],
                                {
                                    name: 'min line on close',
                                    type: 'min',
                                    valueDim: 'close'
                                },
                                {
                                    name: 'max line on close',
                                    type: 'max',
                                    valueDim: 'close'
                                }
                            ]
                        }
                    },
                    {
                        name: 'MA5',
                        type: 'line',
                        data: calculateMA(5),
                        smooth: true,
                        lineStyle: {
                            opacity: 0.5
                        }
                    },
                    {
                        name: 'MA10',
                        type: 'line',
                        data: calculateMA(10),
                        smooth: true,
                        lineStyle: {
                            opacity: 0.5
                        }
                    },
                    {
                        name: 'MA20',
                        type: 'line',
                        data: calculateMA(20),
                        smooth: true,
                        lineStyle: {
                            opacity: 0.5
                        }
                    },
                    {
                        name: 'MA30',
                        type: 'line',
                        data: calculateMA(30),
                        smooth: true,
                        lineStyle: {
                            opacity: 0.5
                        }
                    },

                ]
            };

            option && myChart.setOption(option);



        }


        function macdCh() {//柱状图 
            var chartDom = document.getElementById('macd');
            var myChart = echarts.init(chartDom);
            var option;

            var xAxisData = [];
            var data1 = [];
            var data2 = [];
            for (var i = 0; i < 100; i++) {
                xAxisData.push('类目' + i);
                data1.push((Math.sin(i / 5) * (i / 5 - 10) + i / 6) * 5);
                // data2.push((Math.cos(i / 5) * (i / 5 - 10) + i / 6) * 5);
            }

            option = {
                title: {
                    text: '模拟macd'
                },
                legend: {
                    data: ['bar', 'bar2']
                },
                toolbox: {
                    // y: 'bottom',
                    feature: {
                        magicType: {
                            type: ['stack', 'tiled']
                        },
                        dataView: {},
                        saveAsImage: {
                            pixelRatio: 2
                        }
                    }
                },
                tooltip: {},
                xAxis: {
                    data: xAxisData,
                    splitLine: {
                        show: false
                    }
                },
                yAxis: {
                },
                series: [{
                    name: 'bar',
                    type: 'bar',
                    data: data1,
                    emphasis: {
                        focus: 'series'
                    },
                    animationDelay: function (idx) {
                        return idx * 10;
                    }
                }, {
                    name: 'bar2',
                    type: 'bar',
                    data: data2,
                    emphasis: {
                        focus: 'series'
                    },
                    animationDelay: function (idx) {
                        return idx * 10 + 100;
                    }
                }],
                animationEasing: 'elasticOut',
                animationDelayUpdate: function (idx) {
                    return idx * 5;
                }
            };

            option && myChart.setOption(option);

        }
        macdCh() 

//ema(12)=2/(12+1)=0.1538;EMA(26)=2/27=0.0741



    </script>

</body>

</html>